﻿using FiddlerExUtils.Tools;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;

namespace FiddlerExUtils.Components
{
    public partial class DataGridViewComponent : DataGridView
    {
        private int checkedRowsCount;
        private List<DataGridViewRow> checkedRows;

        public DataGridViewComponent()
        {
            InitializeComponent();
            CommonFunction.DoubleBuffered(this, true);
        }

        public DataGridViewComponent(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            CommonFunction.DoubleBuffered(this, true);
        }

        /// <summary>
        /// 获取第一列选中的行的总数
        /// </summary>
        [Browsable(false)]
        public int CheckedRowsCount
        {
            get
            {
                checkedRowsCount = 0;
                for (int i = 0; i < Rows.Count; i++)
                {
                    //获取控件的值
                    DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)Rows[i].Cells["isChecked"];
                    bool isChecked = Convert.ToBoolean(checkCell.Value);

                    if (isChecked)
                    {
                        checkedRowsCount++;
                    }
                }
                return checkedRowsCount;
            }
        }

        /// <summary>
        /// 获取用户第一列选中的行的集合
        /// </summary>
        [Browsable(false)]
        public List<DataGridViewRow> CheckedRows
        {
            get
            {
                checkedRows = new List<DataGridViewRow>();
                for (int i = 0; i < Rows.Count; i++)
                {
                    //获取控件的值
                    DataGridViewRow checkedRow = Rows[i];
                    bool isChecked = Convert.ToBoolean(checkedRow.Cells["isChecked"].Value);

                    if (isChecked)
                    {
                        checkedRows.Add(checkedRow);
                    }
                }
                return checkedRows;
            }
        }

        private void DataGridViewComponent_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.X)
            {
                this.Rows.Clear();
            }
            GC.Collect();
        }
    }

}
